<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="性别" clearable size="small">
          <el-option
            v-for="dict in genderOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="driverMobile">
        <el-input
          v-model="queryParams.driverMobile"
          placeholder="请输入司机手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="是否接单" prop="orderSwitch">
        <el-select v-model="queryParams.orderSwitch" placeholder="是否接单" clearable size="small">
          <el-option
            v-for="dict in enableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="代驾经验" prop="experience">
        <el-select v-model="queryParams.experience" placeholder="代驾经验" clearable size="small">
          <el-option
            v-for="dict in experienceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="driverList">

      <el-table-column label="头像" align="center" prop="driverAvator">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="80" :src="ossPreview+ scope.row.driverAvator"></el-avatar>
        </template>

      </el-table-column>
      <el-table-column label="姓名" align="center" prop="driverName"/>
      <el-table-column label="性别" :formatter="genderFormat" align="center" prop="gender"/>
      <el-table-column label="手机号" align="center" prop="driverMobile"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>
      <el-table-column label="是否接单" :formatter="switchFormat" align="center" prop="orderSwitch"/>
      <el-table-column label="代价费" align="center" prop="agentFee"/>
      <el-table-column label="代驾经验" :formatter="experienceFormat" align="center" prop="experience"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleChoose(scope.row)"
            v-hasPermi="['order:order:zpDriver']"
          >指派
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代驾司机对话框 -->
  </div>
</template>

<script>
  import {listDriver,driverInfo} from "@/api/driver/driver";
  import {zpDriver} from "@/api/order/order";
  import defaultSettings from '@/settings'

  export default {
    name: "Driver",
    props: {
      orderId: {
        type: Number
      }
    },
    components: {},
    data() {
      return {
        driverId: null,
        ossPreview: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 代驾司机表格数据
        driverList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        destroy: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          gender: undefined,
          driverName: undefined,
          driverMobile: undefined,
          idCard: undefined,
          isEnable: 1,
          orderSwitch: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          gmtTime: undefined
        },
        // 性别数据字典
        genderOptions: [],
        // 状态数据字典
        enableOptions: [],
        // 驾驶经验数据字典
        experienceOptions: [],
        // 表单参数
        form: {
          orderId: this.orderId,
          driverId: undefined
        },
        // 表单校验
        rules: {},
        // 日期范围
        dateRange: []
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_user_sex").then(response => {
        this.genderOptions = response.data;
      });
      this.getDicts("sys_is_enable").then(response => {
        this.enableOptions = response.data;
      });
      this.getDicts("driving_experience").then(response => {
        this.experienceOptions = response.data;
      });
    },
    methods: {
      /** 查询代驾司机列表 */
      getList() {
        this.loading = true;
        listDriver(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.driverList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          gender: undefined,
          driverName: undefined,
          driverAvator: undefined,
          driverMobile: undefined,
          idcardFront: undefined,
          idcardBack: undefined,
          idCard: undefined,
          isEnable: undefined,
          orderSwitch: undefined,
          openId: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          coordinate: undefined,
          locPoi: undefined,
          gmtTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 修改按钮操作 */
      handleChoose(row) {
        this.form.driverId = row.id;
        let  _this = this
        this.$confirm('是否确认指派订单给该代驾司机', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return zpDriver(_this.form);
        }).then(() => {
          this.$emit("emitdriverzpDriver");
        })
      },

      genderFormat(row, column) {
        return this.selectDictLabel(this.genderOptions, row.gender);
      },
      enableFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.isEnable);
      },
      switchFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.orderSwitch);
      },
      experienceFormat(row, column) {
        return this.selectDictLabel(this.experienceOptions, row.experience);
      }
    }
  };
</script>
